package com.wcl.nullmusic.ui.fragment;

import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseFragment;
import com.wcl.nullmusic.mode.PlayMusic;
import com.wcl.nullmusic.ui.activity.MainActivity;
import com.wcl.nullmusic.ui.adapter.MyMusicRecyclerViewAdapter;
import com.wcl.nullmusic.ui.interfaces.MyMusicOnClickListener;
import com.wcl.nullmusic.ui.service.NullMusicService;
import com.wcl.nullmusic.view.DividerItemDecoration;
import com.wcl.nullmusic.utils.LocalMp3InfoUtils;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/5.
 */
public class MyMusicFragment extends BaseFragment implements MyMusicOnClickListener {
    private static final int DATA_REFRESH = 1000;
    private RecyclerView recyclerView;
    private MyMusicRecyclerViewAdapter adapter;
    private MainActivity mainActivity;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case DATA_REFRESH:
                    refreshData();
                    break;
            }
            return false;
        }
    });

    @Override
    protected int getLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void getVaule() {

    }

    @Override
    protected void initView(View view) {
        mainActivity = (MainActivity) getContext();
        recyclerView = (RecyclerView) view.findViewById(R.id.mymusic_recyclerView);
        adapter = new MyMusicRecyclerViewAdapter(getActivity());
    }

    @Override
    protected void initData() {
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        refreshData();
        //  handler.sendEmptyMessage(DATA_REFRESH);
    }

    @Override
    protected void listener() {
        adapter.setOnMyMusicClickListener(this);
    }

    private void refreshData() {
        Log.i("wclwcl", LocalMp3InfoUtils.getMp3Info().size() + "****");
        adapter.addData(LocalMp3InfoUtils.getMp3Info());
    }

    @Override
    public void onResume() {
        super.onResume();
        handler.sendEmptyMessage(DATA_REFRESH);
    }

    @Override
    public void setMyMusicOnClickListener(int position, ImageView imageView) {
        imageView.setVisibility(View.VISIBLE);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
        PlayMusic playMusic = new PlayMusic();
        playMusic.setState(NullMusicService.PLAYER_PLAY);
        playMusic.setPosition(position);
        playMusic.setFlag(false);
        EventBus.getDefault().post(playMusic);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
